<template>
	<view class="container">
		<view class="header">
			<image :src="isLoggedIn ? icon : '/static/default-avatar.png'" class="avatar"></image>
			<text class="username">{{ isLoggedIn ? username : '未登录' }}</text>
		</view>
		<view class="content">
			<view class="entry-group">
				<view class="entry" @click="isLoggedIn ? null : login()">
					<text>{{ isLoggedIn ? '个人信息' : '登录' }}</text>
					<image :src="isLoggedIn ? UserInfo : '/static/login-icon.png'" class="icon"></image>
				</view>
				<view class="entry" @click="isLoggedIn ? null : register()">
					<text>{{ isLoggedIn ? '订单列表' : '注册' }}</text>
					<image :src="isLoggedIn ? purchaseorder : '/static/register-icon.png'" class="icon"></image>
				</view>
				<view v-if="isLoggedIn" class="entry">
					<text>修改密码</text>
					<image :src="modifypassword" class="icon"></image>
				</view>
				<view v-if="isLoggedIn" class="entry">
					<text>意见反馈</text>
					<image :src="yijianfankui" class="icon"></image>
				</view>
			</view>
			<button v-if="isLoggedIn" type="default" @click="logout">退出</button>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				isLoggedIn: false,
				icon: "/static/HAT.jpg",
				username: '',
				user: {
					name: '',
					password: ''
				}
			}
		},
		onShow() {
			this.checkLoginStatus();
		},
		methods: {
			login() {
				uni.request({
					url: 'http://localhost:8090/foodapp/user/login',
					method: 'POST',
					data: this.user,
					success: res => {
						if (res.data.success) {
							uni.setStorageSync('userinfo', res.data.data);
							this.isLoggedIn = true;
							this.username = res.data.data.username;
							this.icon = res.data.data.avatar; // 假设API返回的对象中有'avatar'字段
							uni.showToast({
								title: '' + res.data.data.username,
								icon: 'none'
							});
							uni.switchTab({
								url: '/pages/my/my'
							});
						} else {
							uni.setStorageSync('userinfo', null);
							uni.showToast({
								title: '失败',
								icon: 'none'
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			register() {
				uni.navigateTo({ url: '/pages/my/Register' });
			},
			logout() {
				this.isLoggedIn = false;
				this.username = '';
				this.icon = "/static/HAT.jpg"; // 重置头像
			},
			checkLoginStatus() {
				const userInfo = uni.getStorageSync('userinfo');
				if (userInfo) {
					this.isLoggedIn = true;
					this.username = userInfo.username;
					this.icon = userInfo.avatar; // 假设存储的用户信息中有'avatar'字段
				}
			}
		}
	}
</script>
<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #f8f8f8;
		height: 100vh;
	}
	.header {
		width: 100%;
		height: 200px;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #0eabff;
		background-image: url(/static/bgat.png);
		background-size: cover;
	}
	.avatar {
		width: 100px;
		height: 100px;
		border-radius: 50%;
	}
	.username {
		margin-top: 10px;
		font-size: 20px;
		color: white;
	}
	.content {
		width: 100%;
		padding: 20px;
	}
	.entry-group {
		width: 100%;
		background-color: white;
		border-radius: 10px;
		overflow: hidden;
	}
	.entry {
		display: flex;
		align-items: center;
		height: 50px;
		line-height: 50px;
		border-bottom: 1px solid #f0f0f0;
		font-size: 18px;
		padding: 0 20px;
	}
	.entry:last-child {
		border-bottom: 0;
	}
	.icon {
		width: 20px;
		height: 20px;
	}
	button {
		margin-top: 30px;
		width: 90%;
		height: 50px;
		line-height: 50px;
		background-color: #0eabff;
		color: white;
		font-size: 18px;
		border: none;
		border-radius: 25px;
	}
</style>
